﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VietThuongLMS.ViewModels;
using System.Web.Mvc;
using System.Data.Entity;

/**
 * <Jason>
 * <Date: 20131114>
 * 
 */
namespace VietThuongLMS.Services
{
    public class GiaoVienService
    {

        public List<GiaoVien> GetGiaoVienList()
        {
            LMSDataEntities context = null;
            try
            {
                context = new LMSDataEntities();

                List<GiaoVien> list = context.GiaoViens.Include("LoaiTaiKhoan").Where(m => m.TaiKhoan.MaLoaiTaiKhoan == Utils.Constant.ACCOUNT_ROLE_TEACHER).ToList();
                return list;
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("GiaoVienServices::GetGiaoVienList", ex.StackTrace, ex.Message);
                return null;
            }
            finally{
                context.Dispose();
            }
        }

        public static GiaoVienViewModel GetGiaoVienList(int pageNum, int pageIndex)
        {
            LMSDataEntities context = null;
            try
            {
                context = new LMSDataEntities();

                List<GiaoVien> list = context.GiaoViens.Include("TaiKhoan")
                    .OrderBy(m => m.TaiKhoan.NgayDangKy).Skip(pageNum * (pageIndex - 1)).Take(pageNum).ToList();
                foreach (GiaoVien gv in list)
                {
                    gv.TaiKhoan.LoaiTaiKhoanReference.Load();
                }
                int total = context.GiaoViens.Count();
                return new GiaoVienViewModel(list, total);
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("GiaoVienServices::GetGiaoVienList", ex.StackTrace, ex.Message);
                return new GiaoVienViewModel();
            }
            finally
            {
                context.Dispose();
            }
        }

        /// <summary>
        /// Lay danh sach giao vien theo mon hoc - FE GiaoVien
        /// | Author: NHDuc
        /// | Created Date: 1/1/2014
        /// </summary>
        /// <param name="pageNum"></param>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        public static GiaoVienByMonHocViewModel GetGiaoVienByMonHocList()
        {
            LMSDataEntities context = null;
            try
            {
                context = new LMSDataEntities();
                List<MonHoc> mhList = context.MonHocs.ToList();
                GiaoVienByMonHocViewModel viewModel = new GiaoVienByMonHocViewModel();
                viewModel.monHocList = new List<MonHocForGVList>();
                foreach (MonHoc monhoc in mhList)
                {
                    MonHocForGVList mh = new MonHocForGVList();
                    List<GiaoVien> list = context.GiaoViens.Include("TaiKhoan").Where(g => g.MaMonHoc == monhoc.ID).OrderByDescending(m => m.KinhNghiemGiangDay).ToList();
                    if (list.Count > 0)
                    {
                        mh.monHoc = new MonHoc();
                        mh.monHoc = monhoc;
                        mh.giaovienList = list;
                        viewModel.monHocList.Add(mh);
                    }
                }
                return viewModel;
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("GiaoVienServices::GetGiaoVienByMonHocList", ex.StackTrace, ex.Message);
                return new GiaoVienByMonHocViewModel();
            }
            finally
            {
                context.Dispose();
            }
        }

        public static bool InsertGiaoVien(GiaoVien comp)
        {
            LMSDataEntities context = null;
            try
            {
                context = new LMSDataEntities();

                context.AddToGiaoViens(comp);
                context.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("GiaoVienServices::InsertGiaoVien", ex.StackTrace, ex.Message);
                return false;
            }
            finally
            {
                context.Dispose();
            }
        }

        public static bool DeleteGiaoVien(int maGiaoVien)
        {
            LMSDataEntities context = null;
            try
            {
                context = new LMSDataEntities();

                GiaoVien obj = context.GiaoViens.Where(c => c.ID == maGiaoVien).FirstOrDefault();
                if (obj != null)
                {
                    context.GiaoViens.DeleteObject(obj);
                    context.SaveChanges();
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("GiaoVienServices::DeleteGiaoVien", ex.StackTrace, ex.Message);
                return false;
            }
            finally
            {
                context.Dispose();
            }
        }

        public static GiaoVien GetGiaoVienById(int id)
        {
            LMSDataEntities context = null;
            try
            {
                context = new LMSDataEntities();

                return context.GiaoViens.Include("TaiKhoan").Include("MonHoc").Where(c => c.ID == id).FirstOrDefault();
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("GiaoVientServices::GetGiaoVienById", ex.StackTrace, ex.Message);
                return null;
            }
            finally
            {
                context.Dispose();
            }
        }

        public static bool Edit(GiaoVien newModel)
        {
            LMSDataEntities context = null;
            try
            {
                context = new LMSDataEntities();

                GiaoVien model = context.GiaoViens.Where(c => c.ID == newModel.ID).FirstOrDefault();

                if (model != null)
                {
                    model.HocVan = newModel.HocVan;
                    model.KinhNghiemBieuDien = newModel.KinhNghiemBieuDien;
                    model.KinhNghiemGiangDay = newModel.KinhNghiemGiangDay;
                    model.Avatar = newModel.Avatar;

                    context.SaveChanges();
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("GiaoVienServices::Edit", ex.StackTrace, ex.Message);
                return false;
            }
            finally
            {
                context.Dispose();
            }
        }
    }
}
